//
// Created by 孙代宇 on 2021/12/27.
//

#ifndef UNTITLED2_SQSTACK_H
#define UNTITLED2_SQSTACK_H
#define MAX_SIZE 50

#include "stdlib.h"

class sqStack {

public:
    typedef struct {
        int *value;
        int top;
    } SqStack;

    SqStack stack;

    // 栈判空
    bool IsEmptyStack(SqStack &sqStack) {
        if (sqStack.top == -1) {
            return true;
        } else {
            return false;
        }
    }

    // 入栈
    bool PushStack(SqStack &sqStack, int value) {
        // 栈满返回
        if (sqStack.top == MAX_SIZE - 1) {
            return false;
        }

        sqStack.value[++sqStack.top] = value;
        return true;
    }

    // 出栈
    bool PopStack(SqStack &sqStack, int &value) {
        if (sqStack.top == -1) {
            return NULL;
        }

        value = sqStack.value[sqStack.top--];
        return true;
    }

    // 读栈顶元素
    bool PeekStack(SqStack &sqStack, int &value) {
        if (sqStack.top == -1) {
            return NULL;
        }

        value = sqStack.value[sqStack.top];
        return true;
    }

    // 初始化栈
    bool InitStack(SqStack &stack) {
        stack.value = (int *)malloc(MAX_SIZE * sizeof (int));
        // 防止违规操作获取到脏数据
        for (int i = 0; i < MAX_SIZE; ++i) {
            stack.value[i] = 0;
        }

        stack.top = -1;
        return 1;
    }
};


#endif //UNTITLED2_SQSTACK_H
